package com.ruoyi.classai.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.classai.domain.PxTrainingCourse;
import com.ruoyi.classai.domain.dto.px.PxTrainingCourseDto;
import com.ruoyi.classai.domain.dto.px.PxTrainingTeacherDto;
import com.ruoyi.classai.domain.vo.px.PxTrainingTeacherVo;
import com.ruoyi.classai.service.IEwcTeacherService;
import com.ruoyi.classai.service.IPxTrainingCourseService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 培训课程Controller
 * 
 * @author lcj
 * @date 2025-06-18
 */
@Tag(name = "培训课程接口")
@RestController
@Validated
@RequestMapping("/trainingCourse")
public class PxTrainingCourseController extends BaseController
{
    @Autowired
    private IPxTrainingCourseService pxTrainingCourseService;
    @Autowired
    private IEwcTeacherService teacherService;

    /**
     * 查询培训课程列表
     * @param trainingId 培训ID
     * @return 课程列表
     */
    @Operation(summary = "查询培训课程列表")
    @RequiresPermissions("classai:trainingCourse:list")
    @GetMapping("/list")
    public TableDataInfo list(@NotBlank(message = "培训id不能为空")String trainingId)
    {
        startPage();
        List<PxTrainingCourse> list = pxTrainingCourseService.list(Wrappers.<PxTrainingCourse>lambdaQuery()
                .eq(PxTrainingCourse::getTrainingId,trainingId).eq(PxTrainingCourse::getStatus,Constants.COMMON_SUCCESS)
                .orderByAsc(PxTrainingCourse::getStartTime,PxTrainingCourse::getEndTime));
        return getDataTable(list);
    }

    /**
     * 导出培训课程列表
     */
    @RequiresPermissions("classai:trainingCourse:export")
    @Log(title = "培训课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PxTrainingCourse pxTrainingCourse)
    {
        List<PxTrainingCourse> list = pxTrainingCourseService.list(Wrappers.query(pxTrainingCourse));
        ExcelUtil<PxTrainingCourse> util = new ExcelUtil<PxTrainingCourse>(PxTrainingCourse.class);
        util.exportExcel(response, list, "培训课程数据");
    }

    /**
     * 获取培训课程详细信息
     */
    @Operation(summary = "获取培训课程详细信息")
    @RequiresPermissions("classai:trainingCourse:query")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(pxTrainingCourseService.getTrainingCourseById(id,0));
    }

    /**
     * 新增培训课程
     */
    @Operation(summary = "新增培训课程")
    @RequiresPermissions("classai:trainingCourse:add")
    @Log(title = "新增培训课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody PxTrainingCourseDto pxTrainingCourse)
    {
        pxTrainingCourseService.addTrainingCourse(pxTrainingCourse);
        return AjaxResult.success("添加成功");
    }

    /**
     * 修改培训课程
     */
    @Operation(summary = "修改培训课程")
    @RequiresPermissions("classai:trainingCourse:edit")
    @Log(title = "修改培训课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Valid @RequestBody PxTrainingCourseDto pxTrainingCourse)
    {
        pxTrainingCourseService.editTrainingCourse(pxTrainingCourse);
        return AjaxResult.success("更新成功");
    }

    /**
     * 删除培训课程
     */
    @Operation(summary = "删除培训课程")
    @RequiresPermissions("classai:trainingCourse:remove")
    @Log(title = "删除培训课程", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        Arrays.asList(ids).forEach(d->{
            PxTrainingCourse trainingCourse=new PxTrainingCourse();
            trainingCourse.setId(d);
            trainingCourse.setStatus(Constants.COMMON_DELETE);
            pxTrainingCourseService.updateById(trainingCourse);
        });
        return AjaxResult.success("删除成功");
    }

    /**
     * 新查询训学员列表
     * @param trainingId 培训id
     * @return 学员列表
     */
    @Operation(summary = "新查询训学员列表")
    @GetMapping("/getTrainingTeacherList")
    public TableDataInfo getTrainingTeacherList(@NotBlank(message = "培训id不能为空")String trainingId) {
        startPage();
        List<PxTrainingTeacherVo> list=teacherService.getTrainingTeacherList(trainingId);
        return getDataTable(list);
    }

    /**
     * 新增培训学员
     */
    @Operation(summary = "新增培训学员")
    @Log(title = "新增培训学员", businessType = BusinessType.INSERT)
    @PostMapping("/addTrainingTeacher")
    public AjaxResult addTrainingTeacher(@Valid @RequestBody PxTrainingTeacherDto trainingTeacherDto) {
        teacherService.addTrainingTeacher(trainingTeacherDto);
        return AjaxResult.success("新增成功");
    }

    /**
     * 编辑培训学员
     */
    @Operation(summary = "编辑培训学员")
    @Log(title = "编辑培训学员", businessType = BusinessType.UPDATE)
    @PostMapping("/editTrainingTeacher")
    public AjaxResult editTrainingTeacher(@Valid @RequestBody PxTrainingTeacherDto trainingTeacherDto) {
        teacherService.editTrainingTeacher(trainingTeacherDto);
        return AjaxResult.success("更新成功");
    }

    /**
     * 删除培训学员
     * @param teacherId 老师id
     * @param trainingId 培训id
     * @return 结果
     */
    @Operation(summary = "删除培训学员")
    @Log(title = "删除培训学员", businessType = BusinessType.DELETE)
    @GetMapping("/removeTrainingTeacher")
    public AjaxResult removeTrainingTeacher(@NotBlank(message = "学员id不能为空") String teacherId,@NotBlank(message = "课程id不能为空") String trainingId) {
        teacherService.removeTrainingTeacher(teacherId,trainingId);
        return AjaxResult.success("删除成功");
    }


    /**
     * 导入学员信息信息
     * @param file 学生excel文件
     * @param trainingId 培训ID
     * @return AjaxResult
     */
    @Operation(summary = "导入学员信息信息",tags = "课程管理模块")
    @PostMapping("/importStudent")
    public AjaxResult importStudent(@NotNull(message = "文件不能为空") MultipartFile file, @NotBlank(message = "培训ID不能为空")String trainingId) {
        return AjaxResult.success("success",teacherService.importTeacher(file,trainingId));
    }

    /**
     * 删除课程视频
     * @param videoId 视频ID不能为空
     * @return AjaxResult
     */
    @Operation(summary = "删除课程视频",tags = "课程管理模块")
    @GetMapping("/deleteVideo")
    public AjaxResult deleteVideo(@NotBlank(message = "视频ID不能为空") String videoId) {
        pxTrainingCourseService.deleteVideo(videoId);
        return AjaxResult.success("success");
    }

}
